package com.aide.simplification.codelib.taglib;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;

import org.apache.commons.configuration.ConfigurationException;

import com.aide.simplification.codelib.app.Codelib;
import com.aide.simplification.global.GlobalConfig;
import com.aide.simplification.global.Utils;

public class GetCodesJSON extends HttpServlet {
	private static final long serialVersionUID = 1L;

	public GetCodesJSON() {
		super();
	}

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setContentType("text/html; charset=GBK");
		response.setCharacterEncoding("GBK");
		PrintWriter out = response.getWriter();

		try {
			GlobalConfig gc = new GlobalConfig(Utils.getConfig("codelib.configfile"));
			Codelib codes = new Codelib();
			String codeType = "";
			if (request.getParameter("codeType") != null) {
				codeType = request.getParameter("codeType");
			}
			String mid = "";
			if (request.getParameter("mid") != null) {
				mid = request.getParameter("mid");
				if ("".equals(mid)) {
					if ("clientsort".equals(codeType)) {
						mid = mid.substring(0, mid.lastIndexOf("_"));
					}
				}

				mid = request.getParameter("mid");
			}
			int lv = 2;
			if (request.getParameter("lv") != null) {
				try {
					lv = Integer.parseInt(request.getParameter("lv"));
				} catch (Exception e) {
					lv = 2;
				}
			}
			int maxLv = gc.getInt("codes." + codeType + ".maxlv");
			String flag = "_%";
			if (lv <= maxLv) {
				flag = gc.getString("codes." + codeType + "." + lv);
				if (flag == null) {
					flag = "_%";
				}
			}
			String flagup = "";
			if (lv > 1) {
				flagup = gc.getString("codes." + codeType + "." + (lv - 1));
			}
			String midall = mid;
			if (!"".equals(mid) && !"0".equals(mid)) {
				String sql = "";
				mid = mid.substring(0, flagup.lastIndexOf("_") + 1);
				// System.out.println("|flag:"+flag);
				if (!flag.equals("_%")) {
					flag = flag.substring(0, flagup.lastIndexOf("_") + 1);
					if (flag.equals("")) {
						flag = "_%";
					}
				}
				// System.out.println("mid:"+mid+"|flag:"+flag+"|flagup:"+flagup+"|midall:"+midall);
				sql = "select " + codeType + "id as id," + codeType
						+ "name as name,* from " + codeType + "code where "
						+ codeType + "id like '" + mid + flag + "' and sign=1";
				sql += " and " + codeType + "id<>'" + midall + "'";
				// System.out.println(sql);
				List<Map<String, Object>> list = codes.find(sql);

				if ("clientsort".equals(codeType)) {
					for (Map<String, Object> map : list) {
						map.put("id", map.get("id") + "_"
								+ map.get("Auditingsign"));
					}
				}
				JSONArray json = JSONArray.fromObject(list);
				out.println(json.toString());
			} else {
				out.println("[]");
			}
		} catch (ConfigurationException e1) {
		}
		out.flush();
		out.close();
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doGet(request, response);
	}

}
